import service from "@/utils/request";
import type {
  GetSpuListParams,
  GetSpuListResponse,
  BaseSaleAttrList,
  SpuSaveOrUpdate,
  SpuImageList,
  SpuSaleAttrList,
  SkuAttr,
} from "./model/spuModel";

enum Api {
  getSpuListUrl = "/admin/product",
  getBaseSaleAttrUrl = "/admin/product/baseSaleAttrList",
  postSpuSaleAttrUrl = "/admin/product/saveSpuInfo",
  getSpuImgListUrl = "/admin/product/spuImageList",
  getSpuSaleAttrListUrl = "/admin/product/spuSaleAttrList",
  postSpuSaleAttrListUrl = "/admin/product/updateSpuInfo",
  postAddSkuUrl = "/admin/product/saveSkuInfo",
  deleteSpuAttrUrl = "/admin/product/deleteSpu",
}

export const getSpuListApi = ({
  page,
  limit,
  category3Id,
}: GetSpuListParams) => {
  return service.get<any, GetSpuListResponse>(
    Api.getSpuListUrl + `/${page}/${limit}?category3Id=${category3Id}`
  );
};

export const getBaseSaleAttrListApi = () => {
  return service.get<any, BaseSaleAttrList>(Api.getBaseSaleAttrUrl);
};

// 添加spu属性
export const postSpuSaleAttrApi = (data: SpuSaveOrUpdate) => {
  return service.post<any, null>(Api.postSpuSaleAttrUrl, data);
};

// 获取图片地址
export const getSpuImgListApi = (spuId: number) => {
  return service.get<any, SpuImageList>(Api.getSpuImgListUrl + `/${spuId}`);
};
// 获取属性值列表
export const getSpuSaleAttrListApi = (spuId: number) => {
  return service.get<any, SpuSaleAttrList>(
    Api.getSpuSaleAttrListUrl + `/${spuId}`
  );
};

// 修改spu
export const postSpuSaleAttrListApi = (data: SpuSaveOrUpdate) => {
  return service.post<any, null>(Api.postSpuSaleAttrListUrl, data);
};

// 添加sku属性
export const postAddSkuApi = (data: SkuAttr) => {
  return service.post<any, null>(Api.postAddSkuUrl, data);
};

// 删除SPU属性
export const deleteSpuAttrApi = (spuId: number) => {
  return service.delete<any, null>(Api.deleteSpuAttrUrl + `/${spuId}`);
};
